package tools;

import java.io.*;
import java.util.HashMap;
import java.util.Properties;

public class IniReader {
    private static File file = null;
    private static IniReader reader;



    protected HashMap sections = new HashMap(); 
    private transient String currentSecion; 
    private transient Properties current; 

    public IniReader(File filename)  {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(filename));
        } catch (FileNotFoundException e) {
            //e.printStackTrace();
        }
        if (reader != null) {
            read(reader);
        }
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
           // e.printStackTrace();
        }
    }



    protected void read(BufferedReader reader)  {
String line = new String();
while (true) {
    try {
        if (!((line = reader.readLine()) != null)) {
            break;
        }
    } catch (IOException e) {
     //   e.printStackTrace();
    }
    parseLine(line);
} 
    } 

    protected void parseLine(String line) { 
line = line.trim(); 
        if (line.matches("\\[.*\\]")) { 
            currentSecion = line.replaceFirst("\\[(.*)\\]", "$1"); 
            current = new Properties(); 
            sections.put(currentSecion, current); 
        } else if (line.matches(".*=.*")) { 
 if (current != null) { 
                int i = line.indexOf('='); 
                String name = line.substring(0, i); 
                String value = line.substring(i + 1); 
                current.setProperty(name, value); 
} 
} 
    } 

    public String getValue(String section, String name) { 
Properties p = (Properties) sections.get(section);
        if (p == null) { 
 return null; 
        }
        return p.getProperty(name);
    } 
}